package org.blaznyoght.subtitles.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Arrays;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

public class OpenVideoActionListener implements ActionListener {
	SwingApplication application;
	private static final List<String> EXTS = Arrays.asList(new String[] {
			"avi", "mpg", "mkv" });

	public OpenVideoActionListener(SwingApplication application) {
		this.application = application;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JFileChooser chooser = new JFileChooser();
		FileFilter filter = new FileFilter() {
			@Override
			public boolean accept(File file) {
				boolean ok = false;
				String name = file.getName();
				String ext = name.split("\\.")[name.split("\\.").length - 1];
				for (String expectedExt : EXTS) {
					ok |= ext.equalsIgnoreCase(expectedExt);
				}
				return ok;
			}

			@Override
			public String getDescription() {
				return "*.avi,*.mpg,*.mkv";
			}

		};
		chooser.setFileFilter(filter);
		int returnVal = chooser.showOpenDialog(application.getFrame());
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File movie = chooser.getSelectedFile();
			application.setMovie(movie);
		}
	}
}
